package com.jy.adscm.operation.mscyb.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.mscyb.bean.*;
import com.jy.adscm.operation.mscyb.mapper.mysql.JqzzPartsMaterialListCheckMapper;
import com.jy.adscm.operation.mscyb.mapper.mysql.JqzzPartsMaterialListMapper;
import com.jy.adscm.operation.mscyb.service.JqzzPartsMaterialListCheckService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.operation.mscyb.service.JqzzPartsMaterialListService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-06-02
 */
@Service
public class JqzzPartsMaterialListCheckServiceImpl extends ServiceImpl<JqzzPartsMaterialListCheckMapper, JqzzPartsMaterialListCheck> implements JqzzPartsMaterialListCheckService {
    @Autowired
    JyUserMapper jyUserMapper;
    @Autowired
    JqzzPartsMaterialListMapper jqzzPartsMaterialListMapper;
    @Autowired
    JqzzPartsMaterialListService jqzzPartsMaterialListService;
    @Override
    public boolean updatejqzzPartsMaterialListCheck(List<JqzzPartsMaterialListCheck> jqzzPartsMaterialListChecks, Token token)throws AdscmSystemException {

        for (JqzzPartsMaterialListCheck jqzzPartsMaterialListCheck : jqzzPartsMaterialListChecks) {
            if ("结果".equals(jqzzPartsMaterialListCheck.getName())){
                JqzzPartsMaterialList jqzzPartsMaterialList = jqzzPartsMaterialListMapper.selectById(jqzzPartsMaterialListCheck.getPid());
                if ("1".equals(jqzzPartsMaterialListCheck.getValue())){
                    jqzzPartsMaterialList.setState(1);
                }
                if ("0".equals(jqzzPartsMaterialListCheck.getValue())){

                    //
                    jqzzPartsMaterialList.setState(0);
                }
                jqzzPartsMaterialList.setCreator(token.getUser().getId().toString());
                jqzzPartsMaterialList.setCreateTime(new Date());
                jqzzPartsMaterialListService.updateJqzzPartsMaterrialList(jqzzPartsMaterialList);
            }
            boolean b = super.updateById(jqzzPartsMaterialListCheck);
        }
        //查询到检查的物料

      //  jqzzPartsMaterialList.setState( Integer.valueOf(jqzzPartsMaterialListCheck.getResult()) );
        //修改这个物料
        return true;
    }

    @Override
    public List<JqzzPartsMaterialListCheck> selectByPid(String pid) {
        Wrapper<JqzzPartsMaterialListCheck> wrapper=new EntityWrapper<>();
        wrapper.eq("pid",pid);
        wrapper.orderBy("rank",false);
        List<JqzzPartsMaterialListCheck> jqzzPartsMaterialListChecks = super.selectList(wrapper);
        for (JqzzPartsMaterialListCheck jqzzPartsMaterialListCheck : jqzzPartsMaterialListChecks) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(jqzzPartsMaterialListCheck.getCreator(),null)){
                JyUser jyUser = jyUserMapper.selectById(jqzzPartsMaterialListCheck.getCreator());
                jqzzPartsMaterialListCheck.setCreatorName(jyUser.getName());
            }
        }

        return jqzzPartsMaterialListChecks;
    }
}
